package tech.xs.common.framework.domain.enums;


import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import tech.xs.common.framework.domain.constant.ResultConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用Result code 和 msg
 */
public enum CommonResultEnum implements ApiResultEnum {

    /**
     * 成功
     */
    success(ResultConstant.SUCCESS_CODE, ResultConstant.SUCCESS_MSG),

    /**
     * 未知错误
     */
    unknownError(ResultConstant.UNKNOWN_ERROR_CODE, ResultConstant.UNKNOWN_ERROR_MSG),

    /**
     * 无效参数
     */
    invalidParameter(ResultConstant.INVALID_PARAMETER_CODE, ResultConstant.INVALID_PARAMETER_MSG),

    /**
     * 账号未登录
     */
    noLogin(ResultConstant.NO_LOGIN_CODE, ResultConstant.NO_LOGIN_MSG),

    /**
     * 登陆已经失效
     */
    loginInvalid(ResultConstant.LOGIN_INVALID_CODE, ResultConstant.LOGIN_INVALID_MSG),

    /**
     * 权限不足
     */
    permissionDenied(ResultConstant.PERMISSION_DENIED_CODE, ResultConstant.PERMISSION_DENIED_MSG);

    @EnumValue
    @JsonValue
    @Getter
    private final int value;
    private final String msg;

    private static final Map<Integer, CommonResultEnum> mappings = new HashMap<>(CommonResultEnum.values().length);

    static {
        for (CommonResultEnum item : values()) {
            mappings.put(item.getValue(), item);
        }
    }

    CommonResultEnum(Integer code, String msg) {
        this.value = code;
        this.msg = msg;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static CommonResultEnum create(int value) {
        return mappings.get(value);
    }

    @Override
    public Integer getValue() {
        return value;
    }
}
